module CampaignsHelper
  
  
  def user_participating_in_any_campaign?
    @current_user && !@current_user.active_campaign.is_default
  end
  
  def campaign_meta_for_facebook_like
    html = ""
    html +="<meta property=\"og:url\" content=\"#{root_url}\"/>"
   
    html +="<meta property=\"og:title\" content=\"#{WEBAPP_CONFIG['webapp_name']}\"/>"
    html += "<meta property=\"og:site_name\" content=\"#{WEBAPP_CONFIG['webapp_name']}\"/>"
    html +="<meta property=\"og:image\" content=\"#{campaign_share_image_url}\"/>"
    
  end
  
  def campaign_status(campaign)
    html =""
    if campaign.ended
      html +="<div class=\"user_info_zone\">"
      html +=t(:campaign_ended_date, :end_date=>campaign.end_date)
      html +="</div>"
    elsif !campaign.active?
      html +="<div class=\"user_info_zone\">"
      html +=t(:campaign_start_date, :start_date=>campaign.start_date)
      html +="</div>"
    elsif user_participating?(campaign)
      html +="<div class=\"user_info_zone\">"
      html +=t(:campaign_participating_with_points, :points=>@current_user.active_user_campaign.points)
      html +="</div>"
    end
    
  end
  
  
  def user_participating?(campaign)
    @current_user && @current_user.active_campaign.id == campaign.id
  end
  
  def campaign_participants(campaign)
    html = ""
    if campaign.active?
      if campaign.participant_count == 1
        html += t(:campaign_participant, :participant_count=>campaign.participant_count)
      else
        html += t(:campaign_participants, :participant_count=>campaign.participant_count)
      end  
      html+="&nbsp;"
      html += link_to t(:campaign_see_participants), participants_campaign_url(campaign)  
    elsif campaign.ended
      if campaign.past_all_participant_count == 1
        html += t(:campaign_past_participant, :participant_count=>campaign.past_all_participant_count)
      else
        html += t(:campaign_past_participants, :participant_count=>campaign.past_all_participant_count)
      end
      
      if campaign.winner_id && campaign.winner_id > -1
        user = User.find(campaign.winner_id)
        html += "&nbsp;O vencedor desta campanha foi #{user_name(user)}"
      end
      html += "<p>"
      html += link_to t(:campaign_see_participants), all_participants_campaign_url(campaign)
      html += "</p>"
    end
    html
  end
  
  
  def campaign_dates(campaign)
    if campaign.active?
      t(:campaign_valid_dates, :start_date=>l(campaign.start_date.to_date, :format=>:long), :end_date=>l(campaign.end_date.to_date, :format=>:long))
    elsif campaign.ended
      t(:campaign_ended_date, :end_date=>l(campaign.end_date.to_date, :format=>:long))
    else
      t(:campaign_start_date, :start_date=>l(campaign.start_date.to_date, :format=>:long))
    end
  end
  
  
  #######Messages
  def campaign_publish_wall_comment_vote_user_content_user_message_prompt
    if active_campaign and active_campaign.publish_wall_comment_vote_user_content_user_message_prompt and !active_campaign.publish_wall_comment_vote_user_content_user_message_prompt.blank? 
      active_campaign.publish_wall_comment_vote_user_content_user_message_prompt
    else
      t(:default_publish_wall_comment_vote_user_content_user_message_prompt)
    end
  end
  
  def campaign_publish_wall_comment_vote_user_content_link_text
    if active_campaign and active_campaign.publish_wall_comment_vote_user_content_link_text and !active_campaign.publish_wall_comment_vote_user_content_link_text.blank? 
      active_campaign.publish_wall_comment_vote_user_content_link_text
    else
      t(:default_publish_wall_comment_vote_user_content_link_text)
    end
  end
  
  def campaign_publish_wall_comment_vote_user_content_caption
    if active_campaign and active_campaign.publish_wall_comment_vote_user_content_caption and !active_campaign.publish_wall_comment_vote_user_content_caption.blank? 
      active_campaign.publish_wall_comment_vote_user_content_caption
    else
      t(:default_publish_wall_comment_vote_user_content_caption)
    end
  end
  
  
  
  def campaign_publish_wall_comment_vote_user_content_message
    if active_campaign and active_campaign.publish_wall_comment_vote_user_content_message and !active_campaign.publish_wall_comment_vote_user_content_message.blank? 
      active_campaign.publish_wall_comment_vote_user_content_message
    else
      t(:default_publish_wall_comment_vote_user_content_message)
    end
  end
  
  
  def campaign_publish_stream_user_message
    if active_campaign and active_campaign.publish_wall_user_message and !active_campaign.publish_wall_user_message.blank? 
      active_campaign.publish_wall_user_message
    else
      t(:default_publish_wall_user_message)
    end
  end
  
  def campaign_publish_stream_caption
    if active_campaign and active_campaign.publish_wall_caption and !active_campaign.publish_wall_caption.blank? 
      active_campaign.publish_wall_caption
    else
      t(:default_publish_wall_caption)
    end
  end
  
  
  def campaign_publish_wall_join_now_link_text
    if active_campaign and active_campaign.publish_wall_join_now_link_text and !active_campaign.publish_wall_join_now_link_text.blank? 
      active_campaign.publish_wall_join_now_link_text
    else
      t(:default_publish_wall_join_now_link_text)
    end
  end
  
  def campaign_publish_wall_user_message_prompt
    if active_campaign and active_campaign.publish_wall_user_message_prompt and !active_campaign.publish_wall_user_message_prompt.blank? 
      active_campaign.publish_wall_user_message_prompt
    else
      t(:default_publish_wall_user_message_prompt)
    end
  end
  
  def campaign_publish_wall_auto_publish
    if active_campaign and active_campaign.publish_wall_auto_publish
      true
    else
      false
    end
  end
  
  def campaign_invitation_action_message
    if active_campaign and active_campaign.invitation_action_message and !active_campaign.invitation_action_message.blank? 
      active_campaign.invitation_action_message
    else
      t(:default_invitation_action_message)
    end
    
  end
  def campaign_invitation_content
    if active_campaign and active_campaign.invitation_content and !active_campaign.invitation_content.blank? 
      active_campaign.invitation_content
    else
      t(:default_invitation_content)
    end
  end
  
  def campaign_invitation_type
    if active_campaign and active_campaign.invitation_type and !active_campaign.invitation_type.blank? 
      active_campaign.invitation_type
    else
      t(:default_invitation_type)
    end
    
  end
  def campaign_invitation_message
    if active_campaign and active_campaign.invitation_message and !active_campaign.invitation_message.blank? 
      active_campaign.invitation_message
    else
      t(:default_invitation_message)
    end
    
  end
  
  
  
  
  def campaign_twitter_account_linkable_image
    #if Campaign.default_campaign and Campaign.default_campaign.twitter_account_url and !Campaign.default_campaign.twitter_account_url.empty?
    if active_campaign and active_campaign.twitter_account_url and !active_campaign.twitter_account_url.empty?
      #link_to image_tag('logo_twitter.png'),Campaign.default_campaign.twitter_account_url, {:target=>"_blank"}
      link_to image_tag('logo_twitter.png'),active_campaign.twitter_account_url, {:target=>"_blank"} 
    end
  end
  def campaign_linkedin_account_linkable_image
    if active_campaign and active_campaign.linkedin_account_url and !active_campaign.linkedin_account_url.empty?
      link_to image_tag('logo_linkedin.png'),active_campaign.linkedin_account_url, {:target=>"_blank"} 
    end
  end
  
  def campaign_facebook_account_linkable_image
    if active_campaign and active_campaign.facebook_account_url and !active_campaign.facebook_account_url.empty? 
      link_to image_tag('logo_facebook.png'),active_campaign.facebook_account_url, {:target=>"_blank"} 
    end
  end
  
  def campaign_youtube_account_linkable_image
    if active_campaign and active_campaign.youtube_account_url and !active_campaign.youtube_account_url.empty?
      link_to image_tag('logo_youtube.png'),active_campaign.youtube_account_url, {:target=>"_blank"} 
    end
  end
  
  
  
  def campaign_share_title  
    if active_campaign and active_campaign.share_title and !active_campaign.share_title.empty?
      active_campaign.share_title
    else
      I18n.t(:default_share_tilte)
    end    
  end
  
  def campaign_share_image_url
    if active_campaign and active_campaign.share_image.file?
      image_path(active_campaign.share_image.url(:medium))
    else
      image_path('default_share_img.png')
    end    
  end
  
  
  def campaign_share_description  
    if active_campaign and active_campaign.share_description and !active_campaign.share_description.empty?
      active_campaign.share_description
    else
      I18n.t(:default_share_description)
    end    
  end
  
  def campaign_header_image
    if active_campaign and active_campaign.header_image.file?
      image_path(active_campaign.header_image.url)
    else
      image_path('default_header.png')
    end    
  end
  
  def build_campaign_winner_email(campaign)
    html = ""
    user = User.find(campaign.winner_id)
    winner_points = UserCampaign.find_by_user_id_and_campaign_id(user,campaign).points.to_s
    #html +=user.full_name
    html +=campaign.campaign_winner_mail_page_template.content
    html.gsub("{{winner_full_name}}", user.full_name).
    gsub("{{campaign_name}}",campaign.name).
    gsub("{{winner_campaign_points}}",winner_points).
    gsub("{{campaign_winner_key}}", campaign.winner_key).
    gsub("{{campaign_start_date}}", campaign.start_date.to_s).
    gsub("{{campaign_end_date}}", campaign.end_date.to_s).
    gsub("{{campaign_prize}}", campaign.prize)
    
  end
  
  private 
  def active_campaign
    active_campaign = nil
    active_campaign = @current_user.active_campaign if @current_user
    active_campaign = active_campaign || Campaign.default_campaign
  end
end
